package com.tsingteng.cosfun.ui.message.systemnotification;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.app.AppContext;
import com.tsingteng.cosfun.bean.ListNotificationResp;
import com.tsingteng.cosfun.bean.NotificationBean;
import com.tsingteng.cosfun.bean.SystemActivityResp;
import com.tsingteng.cosfun.bean.SystemBean;
import com.tsingteng.cosfun.utils.AppUtils;
import com.tsingteng.cosfun.utils.ImageUtils;
import com.tsingteng.cosfun.utils.SpanUtils;
import com.tsingteng.cosfun.utils.TimeUtils;
import com.tsingteng.cosfun.utils.ToastUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 2018.8.16
 * 常胜
 * 系统通知列表适配器
 */
public class SystemNotificationAdapter extends BaseQuickAdapter<ListNotificationResp, BaseViewHolder> {
    private Context mContext;
    public SystemNotificationAdapter(Context context) {
        super(R.layout.item_notification);
        this.mContext =context;
    }

    @Override
    protected void convert(BaseViewHolder helper, ListNotificationResp item) {
        helper.setText(R.id.tv_notifi_title, item.getTitle());
        TextView mTvNotification = helper.getView(R.id.tv_notifi_content);
        if ("SUN_AWARD_NOTICE".equals(item.getType())) {
            Gson gson = new Gson();
            SystemActivityResp systemActivityResp = gson.fromJson(item.getContent(), SystemActivityResp.class);
            String[] split = null;
            String[] splitl = null;
            String[] splitReward = null;
            String[] splitAdress = null;
            String[] splitSun = null;
            String content = systemActivityResp.getContent().replaceAll("[${}]","");
            String patternString = "eventName";
            String patternLevel = "rewardLevel";
            String name = "rewardName";
            String adressType = "address";
            String sunType = "sunAward";
            String eventNameLeft = "";
            String eventNameRight = "";
            String rewardLevelLeft = "";
            String rewardLevelRight = "";
            String rewardNameLeft = "";
            String rewardNameRight = "";
            String adressLeft = "";
            String adressRight = "";
            String sunAwardLeft = "";
            String sunAwardRight = "";
            SpanUtils spanUtils =null;
            if (content.contains(patternString)) {
                Pattern pattern = Pattern.compile(patternString);
                split = pattern.split(content);
                eventNameLeft = split[0];
                eventNameRight = split[1];
            spanUtils = new SpanUtils().append(eventNameLeft)
                    .append(systemActivityResp.getEventName())
                    .setSpans(new SystemFoundSpan(mContext,systemActivityResp.getEventName(),systemActivityResp.getActivityPlanningId()))
                    .append(eventNameRight);
            }
            if (content.contains(patternLevel)){
                Pattern patternl = Pattern.compile(patternLevel);
                splitl = patternl.split(content);
                rewardLevelLeft = splitl[0];
                rewardLevelRight = splitl[1];
                if (rewardLevelLeft.contains(patternString)){
                    Pattern pattern = Pattern.compile(patternString);
                    split = pattern.split(rewardLevelLeft);
                    eventNameLeft = split[0];
                    eventNameRight = split[1];
                }
                spanUtils = new SpanUtils().append(eventNameLeft)
                        .append(systemActivityResp.getEventName())
                        .setSpans(new SystemFoundSpan(mContext,systemActivityResp.getEventName(),systemActivityResp.getActivityPlanningId()))
                        .append(eventNameRight)
                        .append(systemActivityResp.getRewardLevel())
                        .append(rewardLevelRight);
            }
            if (content.contains(name)) {
                Pattern patternReward = Pattern.compile(name);
                splitReward = patternReward.split(content);
                rewardNameLeft = splitReward[0];
                rewardNameRight = splitReward[1];
                if (rewardNameLeft.contains(patternString)){
                    Pattern pattern = Pattern.compile(patternString);
                    split = pattern.split(rewardNameLeft);
                    eventNameLeft = split[0];
                    eventNameRight = split[1];
                }
                if (rewardNameLeft.contains(patternLevel)){
                    Pattern patternl = Pattern.compile(patternLevel);
                    splitl = patternl.split(rewardNameLeft);
                    rewardLevelLeft = splitl[0];
                    rewardLevelRight = splitl[1];
                    if (rewardLevelLeft.contains(patternString)){
                        Pattern pattern = Pattern.compile(patternString);
                        split = pattern.split(rewardLevelLeft);
                        eventNameLeft = split[0];
                        eventNameRight = split[1];
                    }
                }
                spanUtils = new SpanUtils().append(eventNameLeft)
                        .append(systemActivityResp.getEventName())
                        .setSpans(new SystemFoundSpan(mContext,systemActivityResp.getEventName(),systemActivityResp.getActivityPlanningId()))
                        .append(eventNameRight)
                        .append(systemActivityResp.getRewardLevel())
                        .append(rewardLevelRight)
                        .append(systemActivityResp.getRewardName())
                        .append(rewardNameRight);
            }
            if (content.contains(adressType)) {
                Pattern patternAdress = Pattern.compile(adressType);
                splitAdress = patternAdress.split(content);
                adressLeft = splitAdress[0];
                adressRight = splitAdress[1];
                if (adressLeft.contains(patternString)){
                    Pattern pattern = Pattern.compile(patternString);
                    split = pattern.split(adressLeft);
                    eventNameLeft = split[0];
                    eventNameRight = split[1];
                }
                if (adressLeft.contains(patternLevel)){
                    Pattern patternl = Pattern.compile(patternLevel);
                    splitl = patternl.split(adressLeft);
                    rewardLevelLeft = splitl[0];
                    rewardLevelRight = splitl[1];
                    if (rewardLevelLeft.contains(patternString)){
                        Pattern pattern = Pattern.compile(patternString);
                        split = pattern.split(rewardLevelLeft);
                        eventNameLeft = split[0];
                        eventNameRight = split[1];
                    }
                }
                if (adressLeft.contains(name)) {
                    Pattern patternReward = Pattern.compile(name);
                    splitReward = patternReward.split(adressLeft);
                    rewardNameLeft = splitReward[0];
                    rewardNameRight = splitReward[1];
                    if (rewardNameLeft.contains(patternString)){
                        Pattern pattern = Pattern.compile(patternString);
                        split = pattern.split(rewardNameLeft);
                        eventNameLeft = split[0];
                        eventNameRight = split[1];
                    }
                    if (rewardNameLeft.contains(patternLevel)){
                        Pattern patternl = Pattern.compile(patternLevel);
                        splitl = patternl.split(rewardNameLeft);
                        rewardLevelLeft = splitl[0];
                        rewardLevelRight = splitl[1];
                        if (rewardLevelLeft.contains(patternString)){
                            Pattern pattern = Pattern.compile(patternString);
                            split = pattern.split(rewardLevelLeft);
                            eventNameLeft = split[0];
                            eventNameRight = split[1];
                        }
                    }
                }
                spanUtils = new SpanUtils().append(eventNameLeft)
                        .append(systemActivityResp.getEventName())
                        .setSpans(new SystemFoundSpan(mContext,systemActivityResp.getEventName(),systemActivityResp.getActivityPlanningId()))
                        .append(eventNameRight)
                        .append(systemActivityResp.getRewardLevel())
                        .append(rewardLevelRight)
                        .append(systemActivityResp.getRewardName())
                        .append(rewardNameRight)
                        .append(systemActivityResp.getAddress())
                        .setSpans(new SystemNotificationSpan(mContext,systemActivityResp.getAddress(),systemActivityResp.getActivityPlanningId()))
                        .append(adressRight);
            }

            if (content.contains(sunType)) {
                Pattern patternSun = Pattern.compile(sunType);
                splitSun = patternSun.split(content);
                sunAwardLeft = splitSun[0];
                sunAwardRight = splitSun[1];
                if (sunAwardLeft.contains(patternString)){
                    Pattern pattern = Pattern.compile(patternString);
                    split = pattern.split(sunAwardLeft);
                    eventNameLeft = split[0];
                    eventNameRight = split[1];
                }
                if (sunAwardLeft.contains(patternLevel)){
                    Pattern patternl = Pattern.compile(patternLevel);
                    splitl = patternl.split(sunAwardLeft);
                    rewardLevelLeft = splitl[0];
                    if (rewardLevelLeft.contains(patternString)){
                        Pattern pattern = Pattern.compile(patternString);
                        split = pattern.split(rewardLevelLeft);
                        eventNameLeft = split[0];
                        eventNameRight = split[1];
                    }
                }
                if (sunAwardLeft.contains(name)){
                    Pattern patternReward = Pattern.compile(name);
                    splitReward = patternReward.split(sunAwardLeft);
                    rewardNameLeft = splitReward[0];
                    if (rewardNameLeft.contains(patternString)){
                        Pattern pattern = Pattern.compile(patternString);
                        split = pattern.split(rewardNameLeft);
                        eventNameLeft = split[0];
                        eventNameRight = split[1];
                    }
                    if (rewardNameLeft.contains(patternLevel)){
                        Pattern patternl = Pattern.compile(patternLevel);
                        splitl = patternl.split(rewardNameLeft);
                        rewardLevelLeft = splitl[0];
                        if (rewardLevelLeft.contains(patternString)){
                            Pattern pattern = Pattern.compile(patternString);
                            split = pattern.split(rewardLevelLeft);
                            eventNameLeft = split[0];
                            eventNameRight = split[1];
                        }
                    }
                }
                if (sunAwardLeft.contains(adressType)){
                    Pattern patternAdress = Pattern.compile(sunAwardLeft);
                    splitAdress = patternAdress.split(content);
                    adressLeft = splitAdress[0];
                    adressRight = splitAdress[1];
                    if (adressLeft.contains(patternString)){
                        Pattern pattern = Pattern.compile(patternString);
                        split = pattern.split(adressLeft);
                        eventNameLeft = split[0];
                        eventNameRight = split[1];
                    }
                    if (adressLeft.contains(patternLevel)){
                        Pattern patternl = Pattern.compile(patternLevel);
                        splitl = patternl.split(adressLeft);
                        rewardLevelLeft = splitl[0];
                        if (rewardLevelLeft.contains(patternString)){
                            Pattern pattern = Pattern.compile(patternString);
                            split = pattern.split(rewardLevelLeft);
                            eventNameLeft = split[0];
                            eventNameRight = split[1];
                        }
                    }
                    if (adressLeft.contains(name)) {
                        Pattern patternReward = Pattern.compile(name);
                        splitReward = patternReward.split(adressLeft);
                        rewardNameLeft = splitReward[0];
                        if (rewardNameLeft.contains(patternString)){
                            Pattern pattern = Pattern.compile(patternString);
                            split = pattern.split(rewardNameLeft);
                            eventNameLeft = split[0];
                            eventNameRight = split[1];
                        }
                        if (rewardNameLeft.contains(patternLevel)){
                            Pattern patternl = Pattern.compile(patternLevel);
                            splitl = patternl.split(rewardNameLeft);
                            rewardLevelLeft = splitl[0];
                            if (rewardLevelLeft.contains(patternString)){
                                Pattern pattern = Pattern.compile(patternString);
                                split = pattern.split(rewardLevelLeft);
                                eventNameLeft = split[0];
                                eventNameRight = split[1];
                            }
                        }
                    }
                }
                spanUtils = new SpanUtils().append(eventNameLeft)
                        .append(systemActivityResp.getEventName())
                        .setSpans(new SystemFoundSpan(mContext,systemActivityResp.getEventName(),systemActivityResp.getActivityPlanningId()))
                        .append(eventNameRight)
                        .append(systemActivityResp.getRewardLevel())
                        .append(rewardLevelRight)
                        .append(systemActivityResp.getRewardName())
                        .append(rewardNameRight)
                        .append(systemActivityResp.getAddress())
                        .setSpans(new SystemNotificationSpan(mContext,systemActivityResp.getAddress(),systemActivityResp.getActivityPlanningId()))
                        .append(adressRight)
                        .append(systemActivityResp.getSunAward())
                        .setSpans(new SystemSunSpan(mContext,systemActivityResp.getSunAward(),systemActivityResp.getActivityPlanningId()))
                        .append(sunAwardRight);
            }

            mTvNotification.setMovementMethod(LinkMovementMethod.getInstance());
            if (null !=spanUtils) {
                mTvNotification.setText(spanUtils.create());
            }else {
                mTvNotification.setText(item.getContent());
            }
        } else {
            mTvNotification.setText(item.getContent());
        }
        long createTime = item.getCreateTime();
        boolean isYesterday = false;
        try {
            isYesterday = TimeUtils.IsYesterday(createTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //将最新系统通知的时间存放好
        if (helper.getAdapterPosition() == 0) {
            AppUtils.setNotificationTime(createTime);
        }
        if (createTime < (60 * 1000 * 5)) {
            helper.getView(R.id.tv_notifi_time).setVisibility(View.GONE);
        } else {
            if (isYesterday){
                helper.setText(R.id.tv_notifi_time, mContext.getResources().getString(R.string.yestoday));
            }else {
                helper.setText(R.id.tv_notifi_time, TimeUtils.formatChatTime(new Date(createTime)));
            }
        }
    }
}
